<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>放大镜</title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }
        
        .zoom-wrap {
            width: 1200px;
            margin: 0 auto;
        }
        
        .box,
        .big_img {
            float: left;
            position: relative;
            margin: 20px;
        }
        
        .box {
            width: 450px;
            height: 450px;
            border: 1px solid;
        }
        
        .box span {
            position: absolute;
            top: 0;
            left: 0;
            width: 300px;
            height: 300px;
            background-color: orange;
            opacity: .5;
            cursor: move;
            display: none;
        }
        
        .box img {
            width: 100%;
            height: 100%;
        }
        
        .big_img {
            width: 500px;
            height: 500px;
            overflow: hidden;
            display: none;
        }
        
        .big_img img {
            position: absolute;
            top: 0;
            left: 0;
        }
    </style>
</head>

<body>
    <div class="zoom-wrap">
        <div class="box" id="box">
            <img src="img/small.jpg" alt="" />
            <span id="mask"></span>
        </div>
        <div class="big_img" id="big_img">
            <img src="img/big.jpg" />
        </div>
    </div>
</body>
<script type="text/javascript">
    var mask = document.getElementById("mask");
    var oBox = document.getElementById("box");
    var bigImgBox = document.getElementById("big_img");
    var b_img = bigImgBox.getElementsByTagName("img")[0];
    oBox.onmouseover = function() {
        mask.style.display = 'block';
        bigImgBox.style.display = 'block';
        document.onmousemove = function(e) {
            var ev = e || window.event;
            var cw = oBox.offsetWidth;
            var ch = oBox.offsetHeight;
            var offW = mask.offsetWidth;
            var offH = mask.offsetHeight;
            // clientX 鼠标距离左侧的位置，clientY 鼠标距离顶部的距离
            var clientx = ev.clientX - oBox.getBoundingClientRect().left - offW / 2;
            var clienty = ev.clientY - oBox.getBoundingClientRect().top - offH / 2;
            // 限制上下左右都出不去
            // 左侧
            if (clientx <= 0) clientx = 0;
            // 上侧
            if (clienty <= 0) clienty = 0;
            // 右侧
            if (clientx > cw - offW) clientx = cw - offW;
            // 下
            if (clienty > ch - offH) clienty = ch - offH;

            mask.style.left = clientx + 'px';
            mask.style.top = clienty + 'px';
            // 大图的移动比例 = 大图片所能移动的距离 / 小盒子的移动距离 
            var scale = (b_img.offsetWidth - bigImgBox.offsetWidth) / (cw - offW);

            b_img.style.left = (-scale * clientx) + 'px';
            b_img.style.top = (-scale * clienty) + 'px';
        }
    }
    oBox.onmouseout = function() {
        mask.style.display = 'none';
        bigImgBox.style.display = 'none';
        document.onmousemove = 0;
    }
</script>

</html>